package site.xuxingcan;

import com.android.ddmlib.IDevice;
import com.android.ddmlib.InstallException;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;

/**
 * Created by xuxingcan on 16-9-4.
 */
public class GUI {
    private static JFrame frame;
    private JButton refersh_button;
    private JButton intsall_button;
    private JList list1;
    private JPanel jpanel;
    private JTextField soft_path;
    private ADB adb;

    public static void main(String[] args) {
        frame = new JFrame("GUI");
        frame.setContentPane(new GUI().jpanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(1000, 600);
        frame.setVisible(true);
    }

    private GUI() {
        refersh_button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (adb == null) {
                    adb = new ADB();
                }
                IDevice[] IDevices = adb.getDevices();
                if (IDevices == null) {
                    dialog("错误", "没有设备连接！");
                    return;
                }
                ArrayList<String> IDeviceList = new ArrayList<String>();
                for (int i = 0; i < IDevices.length; i++) {
                    IDeviceList.add(IDevices[i].getName());
                }
                setList(IDeviceList);
            }
        });
        intsall_button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                intsall_button.setEnabled(false);
                String softPath = soft_path.getText();
                if (softPath.length() == 0) {
                    dialog("错误", "请输入软件路径！");
                    return;
                }
                File softFile = new File(softPath);
                if (!softFile.exists()) {
                    dialog("错误", "软件路径不存在！");
                    return;
                }
                final File[] files = softFile.listFiles();
                if (files.length == 0) {
                    dialog("错误", "软件路径里没有文件！");
                    return;
                }
                IDevice[] IDevices = adb.getDevices();
                successDevices = 0;
                DevicesCount = IDevices.length;
                for (int i = 0; i < IDevices.length; i++) {
                    final IDevice iDevice = IDevices[i];
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < files.length; i++) {
                                try {
                                    iDevice.installPackage(files[i].getAbsolutePath(), true);
                                } catch (InstallException e1) {
                                    e1.printStackTrace();
                                }
                            }
                            installSuccess();
                        }
                    }).start();
                }
            }
        });
    }

    private int successDevices = 0;
    private int DevicesCount = 0;

    private void installSuccess() {
        successDevices++;
        if (DevicesCount == successDevices) {
            intsall_button.setEnabled(true);
        }
    }

    private void setList(ArrayList<String> listKey) {
        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < listKey.size(); i++) {
            model.addElement(listKey.get(i));
        }
        list1.setModel(model);
    }

    private void dialog(String title, String msg) {
        JOptionPane.showMessageDialog(frame.getContentPane(),
                msg, title, JOptionPane.INFORMATION_MESSAGE);
    }
}
